const db = require("../config/db");

module.exports = {
  getAll: () => db.promise().query("SELECT * FROM prizes"),

  getByActivityId: (activityId) =>
    db
      .promise()
      .query("SELECT * FROM prizes WHERE activityId = ?", [activityId]),

  add: (prize) => {
    const { activityId, level, name, quantity, probability, image } = prize;
    return db
      .promise()
      .query(
        "INSERT INTO prizes (activityId, level, name, quantity, probability, image) VALUES (?,?,?,?,?,?)",
        [activityId, level, name, quantity, probability, image || ""]
      );
  },

  update: (prize) => {
    const { id, activityId, level, name, quantity, probability, image } = prize;
    return db
      .promise()
      .query(
        "UPDATE prizes SET activityId=?, level=?, name=?, quantity=?, probability=?, image=? WHERE id=?",
        [activityId, level, name, quantity, probability, image || "", id]
      );
  },

  delete: (id) => db.promise().query("DELETE FROM prizes WHERE id=?", [id]),
};
